package com.lp.compt.services;

import com.lp.compt.pojo.SsCompetitionEntity;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import com.lp.compt.repositories.SsResultsEntityRepository;
import com.lp.compt.pojo.SsResultsEntity;

import java.util.*;

/**
 * 服务类
 *
 * @author
 * @since 1.0.0
 */
@Service
public class SsResultsEntityService {
    @Autowired
    SsResultsEntityRepository ssResultsEntityRepository;
    @Autowired
    SsCompetitionEntityService ssCompetitionEntityService;

    /**
     * 根据SsResultsEntity的字段自动生成条件,字段值为null不生成条件
     * 如果是数值型的字段,前端不传入值,默认是0,例如ID的类型是Long,如果不传值,默认是0
     * 可以自己设置下SFWhere.and(ssResultsEntity).equal(实体.getId()>0,"id",实体.getId()).build()
     *
     * @param ssResultsEntity 实体对象
     * @param pageable        分页对象
     * @return 返回分页\状态码
     */
    public ResponseEntity<Object> search(SsResultsEntity ssResultsEntity, Pageable pageable) {
        Page<SsResultsEntity> all = ssResultsEntityRepository.findAll(Example.of(ssResultsEntity), pageable);
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    public Map save(SsResultsEntity entity) {
        SsCompetitionEntity cmp = ssCompetitionEntityService.findById(entity.getIdCompettion());
        int max = ssResultsEntityRepository.findByCount(entity.getIdCompettion());
        if (cmp != null && max >= cmp.getcNumbers()) {
            return new HashMap() {{
                put("code", 500);
                put("msg", "名额已满");
            }};
        }
        Optional<SsResultsEntity> optional = ssResultsEntityRepository.findOne(Example.of(
                SsResultsEntity.builder().idCompettion(entity.getIdCompettion()).idUser(entity.getIdUser()).build()
        ));
        if (optional.isPresent()) {
            return new HashMap() {{
                put("code", 500);
                put("msg", "您已报名，请勿重复报名");
            }};
        }
        entity.setrZkzh(String.valueOf(Calendar.getInstance().getTimeInMillis()));
        ssResultsEntityRepository.save(entity);
        return new HashMap() {{
            put("code", 200);
            put("msg", "报名成功");
        }};
    }

    public List<SsResultsEntity> searchByWhere(SsResultsEntity ssResultsEntity) {
        return ssResultsEntityRepository.findAll(Example.of(ssResultsEntity));
    }

    public void up(Map<String, Integer> map) {
        Optional<SsResultsEntity> optional = ssResultsEntityRepository.findOne(Example.of(
                SsResultsEntity.builder()
                        .idUser(map.get("userid"))
                        .idCompettion(map.get("id"))
                        .build()
        ));
        if (optional.isPresent()) {
            SsResultsEntity entity = optional.get();
            entity.setcResult(String.valueOf(map.get("up")));
            ssResultsEntityRepository.save(entity);
        }
    }

    public List queryUser(String id) {
        return ssResultsEntityRepository.queryUser(id);
    }
}
